package com.yc.supermarket.web.controller;

import com.yc.supermarket.entity.Supplier;
import com.yc.supermarket.service.SupplierService;
import com.yc.supermarket.service.impl.SupplierServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;

@WebServlet("/supplier")
public class SupplierController extends BaseController {
    
    private SupplierService service = new SupplierServiceImpl();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");
        String op=req.getParameter("op");
        if ("show".equals(op)) {
            doShow(req, resp);
        }else if("findByTrem".equals(op)){
            doFindByTrem(req,resp);
        }else if("add".equals(op)){
            doAdd(req,resp);
        }else if("update".equals(op)){
            doUpdate(req,resp);
        }else{
            resp.getWriter().print("no such method!!");
        }

    }

    protected void doShow(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Supplier sup =parseRequest(req,Supplier.class);
        Map<String,Object> map = service.findByTrem(sup);
        printToJson(resp,map);
    }

    protected void doFindByTrem(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Supplier sup =parseRequest(req,Supplier.class);
        Map<String,Object> map = service.findByTrem(sup);
        printToJson(resp,map);
    }

    protected void doAdd(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Supplier sup = parseRequest(req,Supplier.class);
        boolean result = service.add(sup);
        printToJson(resp,result);
    }

    protected void doUpdate(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Supplier sup = parseRequest(req,Supplier.class);
        boolean result = service.update(sup);
        printToJson(resp,result);
    }
}
